$(function(){
    obj.init();
});

var obj = new JsFun();
function JsFun() {
    'use strict';
    return {
        iId: null,
        inDataGrid: null,
        outDataGrid: null,
        dom: {
            btnIn: null,
            btnOut: null,
            btnClose: null,
        },
        init: function () {
            this.iId     = xhTool.getQuery('iId');
            this.dom.btnIn   = $('#bt_in');
            this.dom.btnOut  = $('#bt_out');
            this.dom.btnClose= window.top.xhTop.getModalCloseDom();
            this.inDataGrid  = new xhDataGrid($('#list_in'), 'Apprelease/getSchoolData', {iId: this.iId, type: 1});
            this.outDataGrid = new xhDataGrid($('#list_out'), 'Apprelease/getSchoolData', {iId: this.iId, type: 2});

            this.inDataGrid.init().activeButton(['bt_out'], [], 'bs');
            this.outDataGrid.parseToolbar().init().activeButton(['bt_in'], [], 'bs');

            xhTool.bindDocKeyDown($('#bt_list_out_search'));
            this.listener();
        },
        listener: function () {
            var $this = this;
            //关联
            $this.dom.btnIn.click(function () {
                $this.funObj().createGroupSchool();
            });
            //取消关联
            $this.dom.btnOut.click(function () {
                $this.funObj().delGroupSchool();
            });
            //关闭时刷新页面
            $this.dom.btnClose.click(function () {
                $this.funObj().reloadGroup();
            });
            //未关联列表查询
            $('#bt_list_out_search').click(function () {
                $this.funObj().searchOutSchool();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                createGroupSchool: function () {
                    var schoolIds = $this.outDataGrid.getSelectValues('iSchoolId');
                    var isPublicClass = $this.outDataGrid.getSelectValues('isPublicClass');
                         if (isPublicClass.indexOf('1')  !== -1){
                            window.top.swal({
                                title: '注意!所选学校在该日期有公开课!确定要选择这些学校吗?',
                                showCancelButton: true,
                                cancelButtonText: '取消',
                                confirmButtonText: '确认',
                                allowOutsideClick: true,
                                showCloseButton: true,
                                showLoaderOnConfirm: true,
                                type: 'warning'
                            }).then(function(isConfirm) {
                                xhExtra.ajaxRequest('Apprelease/modifySchoolList', {iId: $this.iId, schoolId: schoolIds, type: 1}, {}, function(result) {
                                    $this.inDataGrid.reload();
                                    $this.outDataGrid.reload();
                                });
                            }, function(dismiss) {

                            });
                         }else {
                             xhExtra.ajaxRequest('Apprelease/modifySchoolList', {iId: $this.iId, schoolId: schoolIds, type: 1}, {}, function(result) {
                                 $this.inDataGrid.reload();
                                 $this.outDataGrid.reload();
                             });
                         }
                },
                delGroupSchool: function () {
                    var schoolIds = $this.inDataGrid.getSelectValues('iSchoolId');
                    xhExtra.ajaxRequest('Apprelease/modifySchoolList', {iId: $this.iId, schoolId: schoolIds, type: 2}, {}, function(result) {
                        $this.inDataGrid.reload();
                        $this.outDataGrid.reload();
                    });
                },
                reloadGroup: function () {
                    window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                },
                searchOutSchool: function () {
                    $this.outDataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_out_vague'))
                        }
                    });
                }
            }
        }
    }
}